package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.CwOtherIncome;
import com.kgc.collection.pojo.CwOtherIncomeDetail;
import com.kgc.skProvider.dao.CwOtherIncomeMapper;
import com.kgc.skProvider.service.CwOtherIncomeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

@Service
public class CwOtherIncomeServiceImpl implements CwOtherIncomeService {

    @Autowired
    private CwOtherIncomeMapper cwOtherIncomeMapper;

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Override
    public ResponseMsg selectCwOtherIncomeByPage(Map<String, Object> map) {
        logger.info("CwOtherIncomeServiceImpl selectCwOtherIncomeByPage params {}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        PageInfo<CwOtherIncome> pageInfo=new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<CwOtherIncome> list=cwOtherIncomeMapper.selectCwOtherIncomeByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("CwOtherIncomeServiceImpl selectCwOtherIncomeByPage params {}",pageInfo.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectCwOtherIncomeByCode(Map<String, Object> map) {
        logger.info("CwOtherIncomeServiceImpl  selectCwOtherIncomeByCode params {}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        CwOtherIncome cwOtherIncome=cwOtherIncomeMapper.selectCwOtherIncomeByCode(map);
        List<CwOtherIncomeDetail> list=cwOtherIncomeMapper.selectCwOtherIncomeDetailByCode(map);
        cwOtherIncome.setDetails(list);
        responseMsg.setData(cwOtherIncome);
        logger.debug("CwOtherIncomeServiceImpl selectCwOtherIncomeByCode params {}",cwOtherIncome.toString());
         return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateCwOtherIncome(Map<String, Object> map) {
        logger.info(" CwOtherIncomeServiceImpl updateCwOtherIncome params {}",map.toString());
        deleteCwOtherIncome(map);
        ResponseMsg responseMsg=insertCwOtherIncome(map);
        logger.debug("CwOtherIncomeServiceImpl updateCwOtherIncome return {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertCwOtherIncome(Map<String, Object> map) {
        logger.info("CwOtherIncomeServiceImpl insertCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        CwOtherIncome cwOtherIncome=mapper.convertValue( map.get("cwOtherIncome"), CwOtherIncome.class);
        List<?> rawDetails=(List<?>)map.get("details");
        List<CwOtherIncomeDetail> details=mapper.convertValue( rawDetails, new TypeReference<List<CwOtherIncomeDetail>>(){});
        int result=cwOtherIncomeMapper.insertCwOtherIncome(cwOtherIncome);
        try {
            for (CwOtherIncomeDetail cwOtherIncomeDetail : details) {
                cwOtherIncomeMapper.insertCwOtherDetail(cwOtherIncomeDetail);
            }
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("CwOtherIncomeServiceImpl insertCwOtherIncome return {}" ,responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteCwOtherIncome(Map<String, Object> map) {
        logger.info("CwOtherIncomeServiceImpl deleteCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        int result1=cwOtherIncomeMapper.deleteCwOtherIncome(map);
        int result2= cwOtherIncomeMapper.deleteCwOtherDetail(map);
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("CwOtherIncomeServiceImpl deleteCwOtherIncome return {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg checkCwOtherIncome(Map<String, Object> map) {
        logger.info("CwOtherIncomeServiceImpl checkCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        int result=cwOtherIncomeMapper.checkCwOtherIncome(map);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
       logger.debug("CwOtherIncomeServiceImpl checkCwOtherIncome return {} ",responseMsg);
        return responseMsg;
    }
}
